import datetime
from .models import Train
from tensorflow.keras import layers
import tensorflow as tf
import joblib


def axis():
    date = []
    for i in range(24):
        date.append(str(i) + ':00')
    return date


def utils(time, flag):
    data = []
    result = []
    # dt = datetime.datetime.strptime(time, "%Y-%m-%d")
    # time_ = (dt + datetime.timedelta(days=1)).strftime("%Y-%m-%d")
    for i in range(24):
        if i <= 9:
            flag = time + ' 0' + str(i) + ':00:00'
        else:
            flag = time + ' ' + str(i) + ':00:00'
        res = Train.objects.filter(date=flag)
        data.append(res.values())
    for item in data:
        for query in item:
            if flag == 1:
                result.append(query['precipitation'])
            else:
                result.append(query['temperature'])
    return result


def model_load(path):
    window_size = 23
    model = tf.keras.Sequential([
        layers.Input((window_size, 1)),
        layers.Bidirectional(layers.LSTM(64)),

        layers.Dense(32, activation='relu'),
        layers.Dense(1)
    ])
    model.compile(loss="mse", optimizer='adam', metrics=['mae'])
    model.load_weights(path)
    # model.summary()  # 打印模型参数

    return model
